using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using Cysharp.Threading.Tasks;



public class LoadMgr : Singleton<LoadMgr>
{
    //通过异步加载资源 return UniTask
    public async UniTask<T> LoadAsset<T>(string path)
    {
        var handle = Addressables.LoadAssetAsync<T>(path);
        await handle.Task;
        return handle.Result;
    }

    public void LoadAsset<T>(string path, Action<T> callback)
    {
        Addressables.LoadAssetAsync<T>(path).Completed += (handle) =>
        {
            if (handle.Status == AsyncOperationStatus.Succeeded)
            {
                callback?.Invoke(handle.Result);
            }
        };
    }

    //通过异步实例化 return UniTask
    public async UniTask<GameObject> Instantiate(string path)
    {
        var handle = Addressables.InstantiateAsync(path);
        await handle.Task;
        return handle.Result;
    }

    async public UniTask<T> LoadInstance<T>(string path) where T : MonoBehaviour
    {
        var go = await Instantiate(path);
        return go.GetComponent<T>();
    }

    //释放资源
    public void Release(object obj)
    {
        Addressables.Release(obj);
    }

    //释放实例化的资源
    public void ReleaseInstance(GameObject obj)
    {
        Addressables.ReleaseInstance(obj);
    }


}